/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Resources;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace DslEditorPowerToy.Controls
{
    /// <summary>
    /// Base class for definition of all editors hosted in tool-window
    /// </summary>
    public abstract class HostedEditorDefinitionBase : IHostedEditorDefinition
    {
        private Guid _id = Guid.Empty;
        private ResourceManager _resourceManager = null;

        #region Constructors
        protected HostedEditorDefinitionBase(Guid editorId)
        {
            this._id = editorId;
        }
        #endregion

        #region IHostedEditorDefinition Members
        public Guid Id
        {
            get
            {
                return this._id;
            }
        }
        public abstract string Caption
        {
            get;
        }
        public abstract Bitmap Image
        {
            get;
        }
        public abstract Control Control
        {
            get;
        }
        #endregion

        #region Protected Properties
        /// <summary>
        /// Gets the assembly containing the resource file, containing the bitmap containing the editor images
        /// </summary>
        protected abstract Assembly ResourceAssembly
        {
            get;
        }
        /// <summary>
        /// Gets the filename (name and namespace) of the resource file, containing the bitmap containing the editor images
        /// </summary>
        protected abstract string ResourceFileName
        {
            get;
        }
        /// <summary>
        /// Returns the resource manager
        /// </summary>
        protected ResourceManager ResourceManager
        {
            get
            {
                if (null == this._resourceManager)
                {
                    //Initialise instance
                    this._resourceManager = new ResourceManager(this.ResourceFileName, this.ResourceAssembly);
                }
                return this._resourceManager;
            }
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Returns the string from resources for specified resource
        /// </summary>
        /// <param name="resourceId"></param>
        /// <returns></returns>
        protected string GetStringResource(string resourceId)
        {
            if (null == this.ResourceManager)
                return null;

            return this.ResourceManager.GetString(resourceId);
        }
        /// <summary>
        /// Returns sized icon from resources for specified resource
        /// </summary>
        /// <param name="resourceId"></param>
        /// <returns></returns>
        protected Bitmap GetIconResource(string resourceId)
        {
            if (null == this.ResourceManager)
                return null;

            object imageResource = this.ResourceManager.GetObject(resourceId);
            if (null == imageResource)
                return null;

            Bitmap image = imageResource as Bitmap;
            if (null == image)
                return null;

            //Ensure size of image
            return EnsureImageIcon(image);
        }        
        #endregion

        #region Private Methods
        /// <summary>
        /// Ensures image is a proportionate icon
        /// </summary>
        /// <param name="resourceId"></param>
        /// <returns></returns>
        private Bitmap EnsureImageIcon(Bitmap image)
        {
            if (null != image)
            {
                //Calculate correct bounds of icon
                Rectangle rectangle = new Rectangle(0, 0,
                    LeafedHostedEditorControl.EditorImageWidth, LeafedHostedEditorControl.EditorImageHeight);
                if ((rectangle.Right <= image.Width)
                    || (rectangle.Bottom <= image.Height))
                {
                    //Extract right sized icon
                    return image.Clone(rectangle, PixelFormat.DontCare) as Bitmap;
                }
            }

            return image;
        }
        #endregion
    }
}
